#include "TFPusher.h"

void TFPusher::addPush(const std::string& key, const ReqInfo& req)
{
	std::lock_guard<std::mutex> locker(m_mutex);
	m_registerPushes.insert({ key, req });
}

void TFPusher::removePush(const std::string& key, const ReqInfo& req)
{
	std::lock_guard<std::mutex> locker(m_mutex);
	auto iter = m_registerPushes.equal_range(key);
	for (auto iter2 = iter.first; iter2 != iter.second;)
	{
		if (iter2->second.m_webHandler == req.m_webHandler)
			iter2 = m_registerPushes.erase(iter2);
		else
			++iter2;
	}
}

void TFPusher::pushData(const std::string& key, const QString& data)
{
	std::lock_guard<std::mutex> locker(m_mutex);
	auto iter = m_registerPushes.equal_range(key);
	for (auto iter2 = iter.first; iter2 != iter.second; ++iter2)
	{
        emit sendDataToWeb(iter2->second.m_frameId, iter2->second.m_sid, data, iter2->second.m_jsCb);
	}

	if (!key.empty())
	{
		iter = m_registerPushes.equal_range("");
		for (auto iter2 = iter.first; iter2 != iter.second; ++iter2)
		{
            emit sendDataToWeb(iter2->second.m_frameId, iter2->second.m_sid, data, iter2->second.m_jsCb);
		}
	}
}

